<?require_once("connection.php");
//error_reporting(0);
/* $oper = $_POST["oper"];
 $rec = $_POST["rec"];

$name = $_POST["name"];
$year = $_POST["year"];
$semester = $_POST["semester"];
$content = $_POST["content"];
*/

//Reading JSON Request
$course = json_decode($_REQUEST['data'], true);

$oper = $course["oper"];
$rec = $course["rec"];

$name = $course["name"];
$year = $course["year"];
$semester = $course["semester"];
$content = $course["content"];
$instructors = $course["instrustors"];

//Processing the JSON Request
$result = null;
$courseId = 0;
if($oper==0)
{
	$query = "insert into g_courses(name,year,semester,content) values('".$name."', '".$year."', '".$semester."', '".$content."');";
	$result = mysql_query($query, $connection);
	$courseId = mysql_insert_id();
	foreach ($instructors as $instId)
	{
		$subQuery = "insert into g_staff_course(course_id, staff_id) values($courseId, $instId);";
		$result = mysql_query($subQuery, $connection);
	}
}
else
	if($oper==1)
	{
		$query = "update g_courses set name='".$name."', year='".$year."', semester='".$semester."', content='".$content."' where course_id=".$rec.";";
		$result = mysql_query($query, $connection);
	}

	if(empty($result)){
		if($oper==0)
			$message = "<pre>The record has not been ADDED!!!</pre>";
		else
			$message = "<pre>The record has not been EDITED!!!</pre>";
	}
	else{
		if($oper==0)
			$message = "<pre>The record has been successfully ADDED</pre>";
		else
			$message = "<pre>The record has been successfully EDITED</pre>";
	}
	
	if(empty($result)){
		//echo "<pre>Failed to delete data</pre>";
		
		if($oper==0)
			$message =  "<pre>The record has not been ADDED!!!</pre>";
		else
			$message = "<pre>The record has not been EDITED!!!</pre>";
	}
	else{
		//echo "<pre>data deleted</pre>";
		if($oper==0)
			$message = "<pre>The record has been successfully ADDED</pre>";
		else
			$message = "<pre>The record has been successfully EDITED</pre>";
	}
	//JSON response
	//require_once("display_courses.php");
	header('Content-type: text/json');
	header('Content-type: application/json');
	$response = json_encode(array('id' => $courseId,
			'message' => $message));
	$response = json_encode($response);
	die($response);
	?>
